package com.skyz.base.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseRecyclerViewAdapter<DATA,
        VH extends BaseRecyclerViewAdapter.BaseViewHolder>
        extends RecyclerView.Adapter<BaseRecyclerViewAdapter.BaseViewHolder> {

    protected RecyclerView mRecyclerView;
    protected Context mContext;

    private LoadStatus mLoadStatus = LoadStatus.INIT_LOAD;

    private final List<DATA> mDataList = new ArrayList<>();

    private OnItemClickListener<DATA> mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener<DATA> onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnChildItemClickListener(OnChildItemClickListener<DATA> onChildItemClickListener) {
        this.mOnChildItemClickListener = onChildItemClickListener;
    }

    private OnChildItemClickListener<DATA> mOnChildItemClickListener;

    public interface OnChildItemClickListener<DATA> {
        void onChildItemClick(DATA data, View v, int position);
    }

    protected void addChildClick(DATA data, int position, View... vs) {
        if (mOnChildItemClickListener == null || vs == null) {
            return;
        }
        for (View v : vs) {
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnChildItemClickListener == null) {
                        return;
                    }
                    mOnChildItemClickListener.onChildItemClick(data, v, position);
                }
            });

        }


    }


    public List<DATA> getDataList() {
        return mDataList;
    }

    private DATA getData(int position) {
        if (position > mDataList.size() - 1) {
            return null;
        }
        return mDataList.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        switch (mLoadStatus) {
            case INIT_LOAD: {
                return LoadStatus.INIT_LOAD.getViewType();
            }
            case NO_DATA: {
                return LoadStatus.NO_DATA.getViewType();
            }
            case LOAD_ERROR: {
                return LoadStatus.LOAD_ERROR.getViewType();
            }
            default: {
                return getBaseItemViewType(position);
            }
        }
    }

    protected int getBaseItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = new View(parent.getContext());
        LoadStatus loadStatus = LoadStatus.fromViewType(viewType);
        if (loadStatus == null) {
            return onBaseCreateViewHolder(parent, viewType);
        }
        switch (loadStatus) {
            case INIT_LOAD: {
                int layoutId = getInitLoadLayoutId();
                if (layoutId != 0) {
                    view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
                }
                return new BaseViewHolder(view);
            }
            case NO_DATA: {
                int layoutId = getNoDataLayoutId();
                if (layoutId != 0) {
                    view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
                }
                return new BaseViewHolder(view);
            }
            case LOAD_ERROR: {
                int layoutId = getLoadErrorLayoutId();
                if (layoutId != 0) {
                    view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
                }
                return new BaseViewHolder(view);
            }
            default:
                return onBaseCreateViewHolder(parent, viewType);
        }
    }

    protected abstract int getInitLoadLayoutId();

    protected abstract int getNoDataLayoutId();

    protected abstract int getLoadErrorLayoutId();

    protected abstract VH onBaseCreateViewHolder(@NonNull ViewGroup parent, int viewType);

    protected abstract void onBaseBindViewHolder(VH viewHolder, DATA data);

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        int itemViewType = holder.getItemViewType();
        LoadStatus loadStatus = LoadStatus.fromViewType(itemViewType);
        if (loadStatus == null) {
            DATA data = getData(position);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(data, position);
                    }
                }
            });
            onBaseBindViewHolder((VH) holder, data);
            return;
        }
        switch (loadStatus) {
            case INIT_LOAD:
            case NO_DATA: {
                break;
            }
            case LOAD_ERROR: {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //TODO deal loadError
                    }
                });
                break;
            }
        }
    }

    @Override
    public int getItemCount() {
        switch (mLoadStatus) {
            case INIT_LOAD:
            case NO_DATA:
            case LOAD_ERROR: {
                return 1;
            }
            default: {
                return mDataList.size();
            }
        }
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.mRecyclerView = recyclerView;
        this.mContext = mRecyclerView.getContext();
    }

    public void refreshDataList(List<DATA> dataList) {
        mDataList.clear();
        addDataList(dataList);
    }

    public void addDataList(List<DATA> dataList) {
        if (dataList != null) {
            mDataList.addAll(dataList);
        }
        if (mDataList.isEmpty()) {
            mLoadStatus = LoadStatus.NO_DATA;
        } else {
            mLoadStatus = LoadStatus.DATA_LIST;
        }
        notifyDataSetChanged();
    }

    public void addData(DATA data) {
        if (data != null) {
            mDataList.add(data);
        }
        if (mDataList.isEmpty()) {
            mLoadStatus = LoadStatus.NO_DATA;
        } else {
            mLoadStatus = LoadStatus.DATA_LIST;
        }
        notifyDataSetChanged();
    }

    public void loadError() {
        mLoadStatus = LoadStatus.LOAD_ERROR;
        notifyDataSetChanged();
    }

    public interface OnItemClickListener<DATA> {
        void onItemClick(DATA data, int position);
    }

    public enum LoadStatus {
        //初始加载
        INIT_LOAD(0x59421),
        //无数据
        NO_DATA(0x59423),
        //有数据
        DATA_LIST(0x59425),
        //加载失败
        LOAD_ERROR(0x59427),
        ;

        private final int viewType;

        LoadStatus(int viewType) {
            this.viewType = viewType;
        }

        public int getViewType() {
            return viewType;
        }

        public static LoadStatus fromViewType(int viewType) {
            for (LoadStatus loadStatus : LoadStatus.values()) {
                if (loadStatus.getViewType() == viewType) {
                    return loadStatus;
                }
            }
            return null;
        }
    }

    public static class BaseViewHolder extends RecyclerView.ViewHolder {
        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

}
